﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
//using System.Web.UI.WebControls;
using TTSBLL;
using System.Data;
using TTS;
using TTSModel;
using ServiceReference_T0002042;

public partial class T0002039_Add : System.Web.UI.Page
{
    private Assemble a;
    private string message;
    private UnitType u;
    private int versionNum = 6;
    private int versionNum_T0002042 = 2;
    private int versionNum_T0002039 = 4;
    private string T0002039_SN { get { return ViewState["T0002039_SN"].ToString(); } set { ViewState["T0002039_SN"] = value; } }
    private string T0002042_SN { get { return ViewState["T0002042_SN"].ToString(); } set { ViewState["T0002042_SN"] = value; } }
    private string T0002028Left_SN { get { return ViewState["T0002028Left_SN"].ToString(); } set { ViewState["T0002028Left_SN"] = value; } }
    private string T0002028Right_SN { get { return ViewState["T0002028Right_SN"].ToString(); } set { ViewState["T0002028Right_SN"] = value; } }

    private DataRow dr;
    private DataTable dt
    {
        get
        {
            return (DataTable)ViewState["dt"];
        }
        set
        {
            ViewState["dt"] = value;
        }
    }
    public T0002039_Add()
    {
        a = new Assemble();
        u = new UnitType();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dt = new DataTable();
            CommonHelper.AssembleItemDataTableInitialize(dt);
           // assembleVersionListT0002039 = u.LoadEntities("T0002039");
           // this.T0002039_Version.Text = u.UnitType_GetbyTcode("T0002039").TcodeVersion;
          //  this.T0002039_Version.Text = assembleVersionListT0002039[0].TcodeVersion + "," + assembleVersionListT0002039[1].TcodeVersion;
            this.T0002039_Version.Text = u.GetRandomVersion(versionNum_T0002039, "T0002039");
           // this.T0002042_Version.Text = u.UnitType_GetbyTcode("T0002042").TcodeVersion;
            this.T0002042_Version.Text = u.GetRandomVersion(versionNum_T0002042, "T0002042");
           // assembleVersionListT0002028 = u.LoadEntities("T0002028");     
            this.T0002028_LeftVersion.Text = u.GetRandomVersion(versionNum,"T0002028");
            this.T0002028_RightVersion.Text = this.T0002028_LeftVersion.Text;
        }
        this.TextBox_T0002039.Focus();
    }
    protected void Return_Click(object sender, EventArgs e)
    {
        Response.Redirect("RRH.aspx");
    }
    protected void TextBox_T0002039_TextChanged(object sender, EventArgs e)
    {
        T0002039_SN = this.TextBox_T0002039.Text.Trim();
        if (SNHelper.SNCheck(T0002039_SN, "T0002039", out message))
        {
           // if (SNHelper.SNCheckVersion(T0002039_SN, this.T0002039_Version.Text, out message))            
            if (SNHelper.SNCheckVersionRandom(T0002039_SN, this.T0002039_Version.Text, out message))
            {
                if (ProConditionHelper.CheckMainSNsPrecondition(T0002039_SN, "T0002039", out message))
                {
                    this.TextBox_T0002039.Enabled = false;
                    this.TextBox_T0002042.Enabled = true;
                    this.TextBox_T0002042.Focus();
                    return;
                    //this.ConfirmSubmit.Enabled = false;
                }
            }
           
        }
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002039.Text = "";
    }

    protected void TextBox_T0002042_TextChanged(object sender, EventArgs e)
    {
        T0002042_SN = this.TextBox_T0002042.Text.Trim();
        if (SNHelper.SNCheck(T0002042_SN, "T0002042", out message))
        {
            if (SNHelper.SNCheckVersionRandom(T0002042_SN, this.T0002042_Version.Text, out message))
            {
                if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002042_SN, "T0002042", out message))
                {
                    PackageForTektelicServiceSoapClient vsc = new PackageForTektelicServiceSoapClient("PackageForTektelicServiceSoap");
                    if (vsc.IsvalidPackageProductSN(T0002042_SN))
                    {
                        this.TextBox_T0002042.Enabled = false;
                        this.TextBox_T0002028Left.Enabled = true;
                        this.TextBox_T0002028Left.Focus();
                        return;
                    }
                   else
                    {
                        message = "该滤波器未测试或者测试未通过，请重新选择测试通过的滤波器！";
                    }
                }
            }           
        }
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002042.Text = "";
        this.TextBox_T0002042.Focus();
    }
    protected void TextBox_T0002028Left_TextChanged(object sender, EventArgs e)
    {
        T0002028Left_SN = this.TextBox_T0002028Left.Text.Trim();
        if (SNHelper.SNCheck(T0002028Left_SN, "T0002028", out message))
        {
            if (SNHelper.SNCheckVersionRandom(T0002028Left_SN, this.T0002028_LeftVersion.Text, out message))
            {
              if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002028Left_SN, "T0002028", out message))
                {
                   if (ProConditionHelper.IsPassOrFail(T0002028Left_SN, "T0002028_GetStatusPro", out message)) 
                    {

                        this.TextBox_T0002028Left.Enabled = false;
                        this.TextBox_T0002028Right.Enabled = true;
                        this.TextBox_T0002028Right.Focus();
                        return;
                    }
                    /* this.TextBox_T0002028Left.Enabled = false;
                    this.TextBox_T0002028Right.Enabled = true;
                    this.TextBox_T0002028Right.Focus();
                    return;*/
                }
               
            }            
        }
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        this.TextBox_T0002028Left.Text = "";
        this.TextBox_T0002028Left.Focus();
    }

    protected void TextBox_T0002028Right_TextChanged(object sender, EventArgs e)
    {
        T0002028Right_SN = this.TextBox_T0002028Right.Text.Trim();
         if (T0002028Left_SN !=T0002028Right_SN)
        {
            if (SNHelper.SNCheck(T0002028Right_SN, "T0002028", out message))
            {
                if (SNHelper.SNCheckVersionRandom(T0002028Right_SN, this.T0002028_RightVersion.Text, out message))
                {
                    if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002028Right_SN, "T0002028", out message))
                    {
                      if (ProConditionHelper.IsPassOrFail(T0002028Right_SN, "T0002028_GetStatusPro", out message))
                        {
                            this.TextBox_T0002028Right.Enabled = false;
                            this.ConfirmSubmit.Enabled = true;
                            this.ConfirmSubmit.Focus();
                            return;
                        }
                        this.TextBox_T0002028Right.Enabled = false;
                        this.ConfirmSubmit.Enabled = true;
                        this.ConfirmSubmit.Focus();
                        return;
                    }
                }              
            }
           
        }
         else
         {
             message = "T0002028功放板0的SN号与T0002028功放板1的SN号一致，请重新扫描！";
            
         }
         ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
         this.TextBox_T0002028Right.Text = "";
         this.TextBox_T0002028Right.Focus();
    }
    protected void ConfirmSubmit_Click(object sender, EventArgs e)
    {
        if (ProConditionHelper.CheckMainSNsPrecondition(T0002039_SN, "T0002039", out message))
        {
            Unit u = new Unit();
            u.Insert(new UnitInfo() { SN = T0002039_SN, Tcode = "T0002039", IsLeaf = true, IsRoot = true });
            u.Insert(new UnitInfo() { SN = T0002042_SN, Tcode = "T0002042", IsLeaf = true, IsRoot = true });
            u.Insert(new UnitInfo() { SN = T0002028Left_SN, Tcode = "T0002028", IsLeaf = true, IsRoot = true });
            u.Insert(new UnitInfo() { SN = T0002028Right_SN, Tcode = "T0002028", IsLeaf = true, IsRoot = true });

            AssembleInfo ainfo = new AssembleInfo()
            {
                ID = 1,
                SN = T0002039_SN,
                IsRoot = true,
                RecordTime = DateTime.Now,
                Tcode = "T0002039",
                Userid = "111112"
            };
            dt.Rows.Clear();
            dr = dt.NewRow();
            dr["SubSN"] = T0002042_SN;
            dr["Remark"] = "RRH_RadioFrequencyModule";
            dt.Rows.Add(dr);
            /* DataRow  drLeft = dt.NewRow();
               drLeft["SubSN"] = T0002028Left_SN;
               drLeft["Remark"] = "RRH_PowerAmplifierBoardLeft";
             DataRow drRight = dt.NewRow();
               drRight["SubSN"] = T0002028Right_SN;
               drRight["Remark"] = "RRH_PowerAmplifierBoardRight";*/
            dr = dt.NewRow();
            dr["SubSN"] = T0002028Left_SN;
            dr["Remark"] = "RRH_PowerAmplifierBoard_0";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["SubSN"] = T0002028Right_SN;
            dr["Remark"] = "RRH_PowerAmplifierBoard_1";
            dt.Rows.Add(dr);

            if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002042_SN, "T0002042", out message))
            {
                if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002028Left_SN, "T0002028", out message))
                {
                    if (ProConditionHelper.CheckLeafSubSNsPrecondition(T0002028Right_SN, "T0002028", out message))
                    {
                        if (a.Assemble_Insert(ainfo, dt))
                        {
                            message = "录入成功";
                            this.TextBox_T0002039.Text = "";
                            this.TextBox_T0002042.Text = "";
                            this.TextBox_T0002028Left.Text = "";
                            this.TextBox_T0002028Right.Text = "";
                            this.TextBox_T0002039.Enabled = true;
                            this.TextBox_T0002039.Focus();
                            this.ConfirmSubmit.Enabled = false;

                        }
                        else
                        {
                            message = "录入失败，请联系相关人员";
                        }
                    }

                }

            }
        }
       
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
    }

   // public DataRow drLeft { get; set; }
}
